package com.njym.java.security.app.rest;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.njym.java.security.app.biz.FamilyWalletRecordBiz;
import com.njym.java.security.app.entity.FamilyWalletRecord;
import com.njym.java.security.app.entity.FamilyWalletRecordVo;
import com.njym.java.security.common.msg.ObjectRestResponse;
import com.njym.java.security.common.msg.TableResultResponse;
import com.njym.java.security.common.rest.BaseController;
import com.njym.java.security.common.util.ResponseUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("familyWalletRecord")
public class FamilyWalletRecordController extends BaseController<FamilyWalletRecordBiz,FamilyWalletRecord> {

    /**
     * 积分明细
     * @param limit
     * @param page
     * @param userId
     * @return
     */
    @RequestMapping(value = "/getList", method = RequestMethod.GET)
    @ResponseBody
    public Object getList(@RequestParam(defaultValue = "10") int limit,
                                               @RequestParam(defaultValue = "1") int page,
                                               String userId) {
        if (!userId.equals("undefined")) {
            List<Integer> types = new ArrayList();
            types.add(2);
            types.add(3);
            types.add(6);
            Example example = new Example(FamilyWalletRecord.class);
            example.orderBy("modifyTime").desc();
            example.createCriteria().andEqualTo("mechaismId",Integer.parseInt(userId)).andIn("type",types);
            Page<Object> result = PageHelper.startPage(page, limit);
            baseBiz.selectByExample(example);
            return new TableResultResponse<>(result.getPages(), result.getResult());
        } else {
            return ResponseUtil.fail(40301, "错误！");
        }
    }

    /**
     * 兑换记录
     * @param limit
     * @param page
     * @param userId
     * @return
     */
    @RequestMapping(value = "/getHistory", method = RequestMethod.GET)
    @ResponseBody
    public Object getHistory(@RequestParam(defaultValue = "10") int limit,
                                                               @RequestParam(defaultValue = "1") int page,
                                                               String userId) {
        if (!userId.equals("undefined")) {
            Page<Object> result = PageHelper.startPage(page, limit);
            baseBiz.getHistory(Integer.parseInt(userId));
//            Integer total = baseBiz.getHistoryPage(Integer.parseInt(userId));
            Map<String, Object> data = new HashMap<>();
            data.put("list",result.getResult());
            data.put("total",result.getPages());
            return new ObjectRestResponse<FamilyWalletRecordVo>().rel(true).data(data);
        } else {
            return ResponseUtil.fail(40301, "错误！");
        }

    }

    /**
     * 教育专员推广记录
     * @param id
     * @return
     */
    @RequestMapping(value = "/getMyPopularHistory", method = RequestMethod.GET)
    @ResponseBody
    public Object getMyPopularHistory(String id){
        if (!id.equals("undefined")) {
            List<FamilyWalletRecordVo> list = baseBiz.getMyPopularHistory(Integer.parseInt(id));
            return new ObjectRestResponse<FamilyWalletRecordVo>().rel(true).data(list);
        } else {
            return ResponseUtil.fail(40301, "错误！");
        }
    }

}